package com.huatai.range.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.huatai.common.util.JsonUtil;
import com.huatai.common.util.ObjectUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.range.common.utils.ComUtils;
import com.huatai.range.entity.*;
import com.huatai.range.mapper.TrialRangeMapper;
import com.huatai.range.query.TrialRangeQuery;
import com.huatai.range.service.CreateTabService;
import com.huatai.range.service.TrialRangeService;
import com.huatai.range.service.TrialTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static com.huatai.range.common.constant.GISConstant.*;
import static com.huatai.range.common.constant.GISConstant.GIS_EFFECT;

/**
 * @ClassName TrialRangeServiceImpl
 * @Description TODO
 * @Author luoxc
 * @Date 2023/5/5 11:45
 * @Version 1.0
 */
@Service
public class TrialRangeServiceImpl implements TrialRangeService {

	@Autowired
	private TrialTableService trialTableService;

	@Autowired
	private CreateTabService createTabService;

	@Autowired
	private TrialRangeMapper trialRangeMapper;

	/**
	 * 根据试验id查询点、线、面的数据
	 *
	 * @param trialId
	 * @return
	 */
	@Override
	public List<JSONObject> listByTrialId(String trialId) {
		if (StringUtil.isEmpty(trialId)) return null;
		return listRangeByTrialId(trialId);
	}

	/*@Override
	public Object allByTrialId(String trialId) {
		if (StringUtil.isEmpty(trialId)) return null;
		//查询该试验下的表
		List<TrialTableEntity> tableEntities = trialTableService.listByTrialId(trialId);
		List<String> allTabs = createTabService.publicNames();
		//过滤掉不存在的表
		tableEntities = tableEntities.stream().filter(table -> allTabs.contains(table.getTabName())).collect(Collectors.toList());
		if (tableEntities.isEmpty()) return null;
		List<ResultEntity> results = new ArrayList<>();
		tableEntities.forEach(tableEntity -> {
			switch (tableEntity.getTabType()) {
				case GIS_RANGE:
					List<RangeEntity> ranges = trialRangeMapper.allRange(tableEntity.getTabName(), tableEntity.getTrialId());
					if (null != ranges && ranges.size() > 0) {
						ranges.forEach(range -> {
							range.setPoints(strToJson(range.getPoints().toString()));
							results.add(new ResultEntity(range.getTitle(), range));
						});
					}
					break;
				case GIS_PATH:
					List<PathEntity> paths = trialRangeMapper.allPath(tableEntity.getTabName(), tableEntity.getTrialId());
					if (null != paths && paths.size() > 0) {
						paths.forEach(path -> {
							path.setPoints(strToJson(path.getPoints().toString()));
							results.add(new ResultEntity(path.getTitle(), path));
						});
					}
					break;
				case GIS_POI:
					List<PoiEntity> pois = trialRangeMapper.allPoi(tableEntity.getTabName(), tableEntity.getTrialId());
					if (null != pois && pois.size() > 0) {
						pois.forEach(poi -> {
							results.add(new ResultEntity(poi.getTitle(), poi));
						});
					}
					break;
				default:
					break;
			}
		});
		return results;
	}*/

	@Override
	public Object listByTrialIdAndTabType(TrialRangeQuery trialRangeQuery) {
		if (ObjectUtil.isEmpty(trialRangeQuery)) return null;
		//查询该试验下的表
		List<TrialTableEntity> tableEntities = trialTableService.listByTrialIdAndTabType(trialRangeQuery);
		List<String> allTabs = createTabService.publicNames();
		//过滤掉不存在的表
		tableEntities = tableEntities.stream().filter(table -> allTabs.contains(table.getTabName())).collect(Collectors.toList());
		if (tableEntities.isEmpty()) return null;
		List<ResultEntity> results = new ArrayList<>();
		tableEntities.forEach(tableEntity -> {
			switch (tableEntity.getTabType()) {
				case GIS_RANGE:
					JSONObject rangeJson = trialRangeMapper.allRange(tableEntity.getTabName(), tableEntity.getTrialId());
					List<RangeEntity> ranges = JsonUtil.parseArray(rangeJson.get(GIS_DATA).toString(), RangeEntity.class);
					if (ComUtils.listIsNotEmpty(ranges)) {
						ranges.forEach(range -> {
							range.setPoints(strToJson(range.getPoints().toString()));
							results.add(new ResultEntity(range.getTitle(), range));
						});
					}
					break;
				case GIS_PATH:
					JSONObject pathJson = trialRangeMapper.allPath(tableEntity.getTabName(), tableEntity.getTrialId());
					List<PathEntity> paths = JsonUtil.parseArray(pathJson.get(GIS_DATA).toString(), PathEntity.class);
					if (ComUtils.listIsNotEmpty(paths)) {
						paths.forEach(path -> {
							path.setPoints(strToJson(path.getPoints().toString()));
							results.add(new ResultEntity(path.getTitle(), path));
						});
					}
					break;
				case GIS_POI:
					JSONObject poiJson = trialRangeMapper.allPoi(tableEntity.getTabName(), tableEntity.getTrialId());
					List<PoiEntity> pois = JsonUtil.parseArray(poiJson.get(GIS_DATA).toString(), PoiEntity.class);
					if (ComUtils.listIsNotEmpty(pois)) {
						pois.forEach(poi -> {
							results.add(new ResultEntity(poi.getTitle(), poi));
						});
					}
					break;
				default:
					break;
			}
		});
		return results;
	}


	/**
	 * 将json字符串转成JSONArray对象列表
	 *
	 * @param strJson
	 * @return
	 */
	public JSONArray strToJson(String strJson) {
		if (ObjectUtil.isEmpty(strJson)) return null;
		return JSONUtil.parseArray(strJson);
	}

	/**
	 * 查询试验下的点、线、面数据
	 *
	 * @param trialId
	 * @return
	 */
	public List<JSONObject> listRangeByTrialId(String trialId) {
		List<TrialTableEntity> tableEntities = trialTableService.listByTrialId(trialId);
		List<String> allTabs = createTabService.publicNames();
		tableEntities = tableEntities.stream().filter(table -> allTabs.contains(table.getTabName())).collect(Collectors.toList());
		List<JSONObject> results = new ArrayList<>();
		tableEntities.forEach(tableEntity -> {
			switch (tableEntity.getTabType()) {
				case GIS_RANGE:
					results.add(trialRangeMapper.listRange(tableEntity.getTabName(), tableEntity.getTrialId()));
					break;
				case GIS_PATH:
					results.add(trialRangeMapper.listPath(tableEntity.getTabName(), tableEntity.getTrialId()));
					break;
				case GIS_POI:
					results.add(trialRangeMapper.listPoi(tableEntity.getTabName(), tableEntity.getTrialId()));
					break;
				case GIS_EFFECT:
					results.add(trialRangeMapper.listEffect(tableEntity.getTabName(), tableEntity.getTrialId()));
					break;
				default:
					break;
			}
		});
		results.forEach(json -> {
			String data = json.get(GIS_DATA).toString();
			if (!StringUtil.isEmpty(data)) {
				json.put(GIS_DATA, JSONUtils.parse(data));
			}
		});
		return results;
	}

}
